package com.mooc.yohoshop.ui.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.dao.UserDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.UserEntity;
import com.mooc.yohoshop.filed.EventAction;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.gesturelockview.GesturelockActivity;
import com.mooc.yohoshop.ui.verifycode.VerifyCodeActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

public class LoginActivity extends BaseActivity<LoginRepo> implements View.OnClickListener {

    private EditText mMobileEt,mPwdEt;

    @Override
    protected int layout() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        f(R.id.login_back).setOnClickListener(this::onClick);
        f(R.id.mLoginBtn).setOnClickListener(this::onClick);
        f(R.id.mForgetPwdTv).setOnClickListener(this::onClick);
        f(R.id.login_verify_code_tv).setOnClickListener(this::onClick);
        mMobileEt = f(R.id.mMobileEt);
        mPwdEt = f(R.id.mPwdEt);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginRefresh(EventAction action){
        if (action.equals("OK")){
            HashMap<String,Object>map=new HashMap<>();
            map.put("name","xjn");
            map.put("pwd","123456");
            request(repo.requestLoginNP(map));
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) baseEntity;
            new UserDaoEntity().insert(userEntity.data);
            EventAction action = new EventAction();
            action.action = "refreshUser";
            EventBus.getDefault().post(action);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.login_back == id){
            finish();
        }else if (R.id.mLoginBtn == id){
            if (TextUtils.isEmpty(mMobileEt.getText())){
                ToastUtils.showShort("请输入用户名");
                return;
            }
            if (TextUtils.isEmpty(mPwdEt.getText())){
                ToastUtils.showShort("请输入用户名");
                return;
            }
            HashMap<String,Object>map=new HashMap<>();
            map.put("name",mMobileEt.getText().toString());
            map.put("pwd",mPwdEt.getText().toString());
            request(repo.requestLoginNP(map));
        }else if (R.id.login_verify_code_tv == id){
            startActivity(new Intent(this, VerifyCodeActivity.class));
        }else {
            startActivity(new Intent(this, GesturelockActivity.class));
        }
    }
}
